import java.sql.*;
import org.postgresql.Driver;
import java.util.concurrent.TimeUnit;

public class test_jdbc_record_nettime_case0019 {
    public static void main(String[] args) throws Exception {
        System.out.println(Driver.getGSVersion());
        String url = "jdbc:postgresql://" + args[0] + "/" + args[1];
        String username = args[2];
        String password = args[3];
        String sql;

        System.out.println("-----新建连接-----");
        Connection conn = GetConnection(username, password, url);

        System.out.println("-----建表-----");
        try {
            sql = "create table t_jdbc_record_nettime_case0019(id int);";
            System.out.println(sql);
            PreparedStatement rn_create = conn.prepareStatement(sql);
            rn_create.execute();
        } catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
        }

        System.out.println("-----插入数据-----");
        try {
            sql = "insert into t_jdbc_record_nettime_case0019 values(2);";
            System.out.println(sql);
            PreparedStatement rn_insert = conn.prepareStatement(sql);
            rn_insert.execute();
        } catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
        }

        System.out.println("-----更新数据-----");
        try {
            sql = "update t_jdbc_record_nettime_case0019 set id = 666;";
            System.out.println(sql);
            PreparedStatement rn_update = conn.prepareStatement(sql);
            rn_update.execute();
        } catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
        }

        System.out.println("-----删除表-----");
        try {
            sql = "drop table t_jdbc_record_nettime_case0019 cascade;";
            System.out.println(sql);
            PreparedStatement rn_drop = conn.prepareStatement(sql);
            rn_drop.execute();

        } catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
        }

        System.out.println("-----查看version版本-----");
        Statement statement = conn.createStatement();
        String res;
        try (ResultSet resultSet = statement.executeQuery("select version()")){
            if (resultSet.next()){
                res  = resultSet.getObject(1).toString();
                System.out.println(res);}
        } catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
        }
        conn.close();
    }

    public static Connection GetConnection(String username, String passwd, String sourceURL) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(sourceURL, username, passwd);
            System.out.println(sourceURL);
            System.out.println("Connection succeed");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return conn;
    }
}